package com.wyt.common.logger.builder;

import java.util.Date;

/**
 * @ClassName LogRecordBuilder
 * @Description: 日志追踪工具类
 * @Author wangyongtao
 * @Date 2023/6/17 19:49
 * @Version 1.0
 **/
public class LogRecordBuilder {
    /**
     * 线程局部变量中用于存储日志信息的实体对象
     */
    private GwOperLog operLog = new GwOperLog();

    /**
     * 线程局部变量记录整个线程执行过程中要收集的日志信息
     * 注意：如果日志记录的整个链路出现了线程继承情况，要使用继承类的局部变量
     */
    private static final ThreadLocal<LogRecordBuilder> operLogThreadLocal = new ThreadLocal<>();

    /**
     * 获取日志构建对象
     */
    public static LogRecordBuilder builder() {
        LogRecordBuilder logRecordBuilder = operLogThreadLocal.get();
        if (logRecordBuilder == null) operLogThreadLocal.set(new LogRecordBuilder());
        return operLogThreadLocal.get();
    }

    public static GwOperLog get() {
        return operLogThreadLocal.get().operLog;
    }

    public static void remove() {
        operLogThreadLocal.remove();
    }

    /************************************************ 具体字段构造方法（构造方法名尽量和底层存储对象的方法名相同） ***************************************************************/

    public LogRecordBuilder setUser(String user) {
        operLog.setUser(user);
        return this;
    }

    public LogRecordBuilder setCreateDate() {
        operLog.setCreateDate(new Date());
        return this;
    }


}